var opened, activeta;
$(document).ready(function(){
     
    
    
    $('.btnSignIn').click(function(e){
        $('.signInHolder').toggle('slide');
        e.preventDefault();
    });
    
    $('.signIn .close').click(function(e){
        $('.signInHolder').toggle('slide');
        e.preventDefault();
    });
    
    $('.categoryArea a.jClose').click(function(e){
        $('.categoryArea').slideUp();
        e.preventDefault();
    });
    
    $('.navigation li a').click(function(e){
        var url = $(this).attr('href');
        var slug = url.substr(url.lastIndexOf('/') + 1);
        if(slug!=opened) {
            $('.sub-'+opened).slideUp();
        }
        $('.sub-'+slug).toggle('slide');
        opened = slug;
        if($('.sub-'+slug).length) e.preventDefault();
    });
    
    //$(".bottomGallery li img").tipTip();
    $("a.fancy").fancybox();
    
    $('.gotoComment').click(function(){
        $.scrollTo('.commentList', 800);
    });
    
    
    drawVirtualKeyboard();
    $('#keyb').easydrag(true);
    
    // load keyboards
    $(".bangla").each(function(e,input){
        activeta=$(input).attr("id");
        if(activeta) {
            setBanglaContextMenu(activeta);
            eval('make'+kb+'Editor(activeta)');
        }
    });
    
    $('#contentSearch').focus(function(){
        if($(this).val() == 'বাংলায় কীওয়ার্ড লিখুন')($(this).val(''));
    });
    
    $('#contentSearch').blur(function(){
        if($(this).val() == '')($(this).val('বাংলায় কীওয়ার্ড লিখুন'));
    });
    
    $('#bloggerSearch').focus(function(){
        if($(this).val() == 'ব্লগারের নাম লিখুন')($(this).val(''));
    });
    
    $('#bloggerSearch').blur(function(){
        if($(this).val() == '')($(this).val('ব্লগারের নাম লিখুন'));
    });
    
    
    // Registration Page
    $('.regBlog').focus(function(){
        if($(this).val() == 'ব্লগের শিরোনাম')($(this).val(''));
    });
    
    $('.regBlog').blur(function(){
        if($(this).val() == '')($(this).val('ব্লগের শিরোনাম'));
    });
    
    $('.regName').focus(function(){
        if($(this).val() == 'নাম (বাংলায়)')($(this).val(''));
    });
    
    $('.regName').blur(function(){
        if($(this).val() == '')($(this).val('নাম (বাংলায়)'));
    });
    
    $('.regLocation').focus(function(){
        if($(this).val() == 'অবস্থান')($(this).val(''));
    });
    
    $('.regLocation').blur(function(){
        if($(this).val() == '')($(this).val('অবস্থান'));
    });
    
    $('.regDetails').focus(function(){
        if($(this).val() == 'বিস্তারিত')($(this).val(''));
    });
    
    $('.regDetails').blur(function(){
        if($(this).val() == '')($(this).val('বিস্তারিত'));
    });
    
    //$('#commentBox').markItUp(mySettings);
    
    $(".popupbox").fancybox({
        'width'         : '75%',
        'height'        : '75%',
        'autoDimensions': false,
        'autoScale'     : false,
        'titleShow'     : false,
        'transitionIn'  : 'elastic',
        'transitionOut' : 'elastic',
        'type'          : 'iframe'
    });
    
    $(".sendMessage").fancybox({
        'centerOnScroll': true,
        'autoDimensions': false,
        'titleShow'     : false,
        'transitionIn'  : 'elastic',
        'transitionOut' : 'elastic',
        'type'          : 'iframe'
    });
    
    //parseYT();
    //parseMP3();
    
    if($('.frontPagePosts').length){
       $('.frontPagePosts ul').css({overflow:'hidden',width:300,height:30})
       
    }
    
    
});

function insertEmo(id) {
    var txt="";
    switch(id) {
        case 1:
        txt = ":)";
        break;
        case 2:
        txt = ":(";
        break;
        case 3:
        txt = ";)";
        break;
        case 4:
        txt = ":-D";
        break;
        case 5:
        txt = ";;)";
        break;
        case 6:
        txt = ">:D<";
        break;
        case 7:
        txt = ":-/";
        break;
        case 8:
        txt = ":x";
        break;
        case 9:
        txt = ":\">";
        break;
        case 10:
        txt = ":P";
        break;
        case 11:
        txt = ":-*";
        break;
        case 12:
        txt = "=((";
        break;
        case 13:
        txt = ":-O";
        break;
        case 14:
        txt = "X(";
        break;
        case 15:
        txt = ":>";
        break;
        case 16:
        txt = "B-)";
        break;
        case 17:
        txt = ":-S";
        break;
        case 18:
        txt = "!";
        break;
        case 19:
        txt = ">:)";
        break;
        case 20:
        txt = ":'(";
        break;
        case 21:
        txt = ":))";
        break;
        case 22:
        txt = ":|";
        break;
        case 23:
        txt = "/:)";
        break;
        case 24:
        txt = "=))";
        break;
        case 25:
        txt = ":-)";
        break;
        case 26:
        txt = ":-B";
        break;
        case 27:
        txt = "=;";
        break;
        case 28:
        txt = "I-)";
        break;
        case 29:
        txt = "8-)";
        break;
        case 30:
        txt = "L-)";
        break;
        case 31:
        txt = ":-&";
        break;
        case 32:
        txt = ":-$";
        break;
        case 33:
        txt = "[-(";
        break;
        case 34:
        txt = ":O)";
        break;
        case 35:
        txt = "8-}";
        break;
        case 36:
        txt = "<:-P";
        break;
        case 37:
        txt = "(:|";
        break;
        case 38:
        txt = "=P~";
        break;
        case 39:
        txt = ":-?";
        break;
        case 40:
        txt = "#-o";
        break;
        case 41:
        txt = "=D>";
        break;
        case 42:
        txt = ":-SS";
        break;
        case 43:
        txt = "@-)";
        break;
        case 44:
        txt = ":^o";
        break;
        case 45:
        txt = ":-w";
        break;
        case 46:
        txt = ":-<";
        break;
        case 47:
        txt = ">:P";
        break;
        case 48:
        txt = "<):)";
        break;
        case 49:
        txt = ":@)";
        break;
        case 50:
        txt = ":-O";
        break;
        case 51:
        txt = ":(|)";
        break;
        case 52:
        txt = "~:>";
        break;
        case 53:
        txt = "@};-";
        break;
        case 54:
        txt = "%%-";
        break;
        case 55:
        txt = "**==";
        break;
        case 56:
        txt = "(~~)";
        break;
        case 57:
        txt = "~o)";
        break;
        case 58:
        txt = "*-:)";
        break;
        case 59:
        txt = "8-X";
        break;
        case 60:
        txt = "=:)";
        break;
        case 61:
        txt = ">-)";
        break;
        case 62:
        txt = ":-L";
        break;
        case 63:
        txt = "[-O<";
        break;
        case 64:
        txt = "$-)";
        break;
        case 65:
        txt = ":-\"";
        break;
        case 66:
        txt = "-(";
        break;
        case 67:
        txt = ":)>-";
        break;
        case 68:
        txt = "[-X";
        break;
        case 69:
        txt = "\:D/";
        break;
        case 70:
        txt = ">:/";
        break;
        case 71:
        txt = ";))";
        break;
        case 72:
        txt = "o->";
        break;
        case 73:
        txt = "o=>";
        break;
        case 74:
        txt = "o-+";
        break;
        case 75:
        txt = "(%)";
        break;
        case 76:
        txt = ":-@";
        break;
        case 77:
        txt = "^:)^";
        break;
        case 78:
        txt = ":-j";
        break;
        case 79:
        txt = "(*)";
        break;
        case 80:
        txt = ":)]";
        break;
        case 81:
        txt = ":-C";
        break;
        case 82:
        txt = "~x(";
        break;
        case 83:
        txt = ":-h";
        break;
        case 84:
        txt = ":-t";
        break;
        case 85:
        txt = "8->";
        break;
        case 86:
        txt = ":-??";
        break;
        case 87:
        txt = "%-(";
        break;
        case 88:
        txt = ":o3";
        break;
        case 89:
        txt = "X_X";
        break;
        case 90:
        txt = ":!!";
        break;
        case 91:
        txt = "\m/";
        break;
        case 92:
        txt = ":-Q";
        break;
        case 93:
        txt = ":-bd";
        break;
        case 94:
        txt = "^#(^";
        break;
        case 95:
        txt = ":bz";
        break;
        case 96:
        txt = ":ar!";
        break;
    }
    $.markItUp({ replaceWith: txt} );
}

function commentReply(id) {
    $('#CommentParentId').val(id);
    $.scrollTo('.commentAreaStart', 800);
}

function deleteComment(id) {
    $('.comment-'+id).prepend('<div class="ajax-loader"><div></div></div>');
    $('.comment-'+id+' .ajax-loader').fadeIn();
    $.post(BASE+"/ajax/comments/delete", 
        {id: id},
        function(data){
            if(data) {
                if(data == 'done') {
                    $('.comment-'+id).slideUp('slow');
                } else {
                    alert('Something went wrong.');
                    $('.comment-'+id+' .ajax-loader').fadeOut();
                }
            }
        },
        "html"
    );
}

function addToFav(id, type) {
    var c = 'posts';
    if(type === 2) c = 'users';
    
    $.post(BASE+"/ajax/"+c+"/add_to_fav", 
        {id: id},
        function(data){
            if(data) {
                if(data == 'done') {
                    $('.jFavAdd').slideUp('slow', function() {
                        $('.jFavRemove').slideDown('slow');
                    });
                } else {
                    alert('Something went wrong.');
                }
            }
        },
        "html"
    );
}

function removeFromFav(id, type) {
    var c = 'posts';
    if(type === 2) c = 'users';
    
    $.post(BASE+"/ajax/"+c+"/remove_from_fav", 
        {id: id},
        function(data){
            if(data) {
                if(data == 'done') {
                    $('.jFavRemove').slideUp('slow', function() {
                        $('.jFavAdd').slideDown('slow');
                    });
                } else {
                    alert('Something went wrong.');
                }
            }
        },
        "html"
    );
}

function makeSticky(id) {
    var val = $.trim($('.panel-'+id+' input.small').val());
    if(val=='') {
        alert('Please enter a value');
        return false;
    }
    $('.panel-'+id).prepend('<div class="ajax-loader"><div></div></div>');
    $('.panel-'+id+' .ajax-loader').fadeIn();
    $.post(BASE+"/ajax/posts/make_sticky", 
        {id: id, position: $('.panel-'+id+' select:first').val(), value: val, unit: $('.panel-'+id+' select:last').val()},
        function(data){
            if(data) {
                if(data == 'done') {
                    //location.reload(true);
                    window.location = BASE;
                } else {
                    alert('Something went wrong.');
                    $('.panel-'+id+' .ajax-loader').fadeOut();
                }
            }
        },
        "html"
    );
}

function ratePost(id) {
    
    $('.ratingPanel-'+id).prepend('<div class="ajax-loader"><div></div></div>');
    $('.ratingPanel-'+id+' .ajax-loader').fadeIn();
    $.post(BASE+"/ajax/posts/rate_post", 
        {id: id, value: $('.ratingPanel-'+id+' select').val()},
        function(data){
            if(data) {
                if(data == 'done') {
                    //location.reload(true);
                    window.location = BASE;
                } else {
                    alert('Something went wrong.');
                    $('.ratingPanel-'+id+' .ajax-loader').fadeOut();
                }
            }
        },
        "html"
    );
}

function makeFeatured(id) {
    $.post(BASE+"/ajax/posts/make_featured", 
        {id: id},
        function(data){
            if(data) {
                
                if(data == 'done') {
                    //location.reload(true);
                    window.location = BASE;
                } else {
                    alert('Something went wrong.');
                }
            }
        },
        "html"
    );
}
function removeFeatured(id) {
    $.post(BASE+"/ajax/posts/make_featured/false", 
        {id: id},
        function(data){
            if(data) {
                
                if(data == 'done') {
                    //location.reload(true);
                    window.location = BASE;
                } else {
                    
                    alert('Something went wrong.');
                    alert(data)
                }
            }
        },
        "html"
    );
}

function banUser(id) {
    $('.stickyPanel').prepend('<div class="ajax-loader"><div></div></div>');
    $('.stickyPanel .ajax-loader').fadeIn();
    $.post(BASE+"/ajax/users/ban", 
        {id: id, type: $('.stickyPanel select').val(), reason: $('.stickyPanel textarea').val()},
        function(data){
            if(data) {
                if(data == 'done') {
                    $('.stickyHolder').toggle('slide');
                } else {
                    alert('Something went wrong.');
                    $('.stickyPanel .ajax-loader').fadeOut();
                }
            }
        },
        "html"
    );
}

function banBlogUser(id) {
    $('.stickyPanel').prepend('<div class="ajax-loader"><div></div></div>');
    $('.stickyPanel .ajax-loader').fadeIn();
    $.post(BASE+"/ajax/users/user_ban", 
        {id: id},
        function(data){
            if(data) {
                if(data == 'done') {
                    $('.stickyHolder').toggle('slide');
                    $('.user-'+id+' .ban').html('| ব্লক করা হয়েছে');
                } else {
                    alert('Something went wrong.');
                    $('.stickyPanel .ajax-loader').fadeOut();
                }
            }
        },
        "html"
    );
}

function unbanBlogUser(id) {
    $('.stickyPanel').prepend('<div class="ajax-loader"><div></div></div>');
    $('.stickyPanel .ajax-loader').fadeIn();
    $.post(BASE+"/ajax/users/user_unban", 
        {id: id},
        function(data){
            if(data) {
                if(data == 'done') {
                    $('.stickyHolder').toggle('slide');
                    $('.user-'+id+' .ban').html('| আনব্লক করা হয়েছে');
                } else {
                    alert('Something went wrong.');
                    $('.stickyPanel .ajax-loader').fadeOut();
                }
            }
        },
        "html"
    );
}

function archiveSearch() {
    var month, year;
    month = $('.selectArea #month').val();
    if(month==undefined) {
        
        var date = $('.selectArea #date').val();
        if(date=='') {
            alert('Please select any date from the dropdown');
            return false;
        }
        
        var user = $('.selectArea #user').val();
        if(user=='') {
            alert('Something went wrong, try to refresh the page');
            return false;
        }
        
        month = date.substring(0, date.indexOf('-'));
        year = date.substring(date.indexOf('-') + 1);
        
        window.location = BASE + '/archives/' + user + '/' + year + '/' + month;
        
    } else {
        
        if(month=='') {
            alert('Please select any month from the dropdown');
            return false;
        }
        
        year = $('.selectArea #year').val();
        if(year=='') {
            alert('Please select any year from the dropdown');
            return false;
        }
        window.location = BASE + '/archives/' + year + '/' + month;
        
    }
}